import axios from 'axios';
import { axiosConfig } from './config';
import api from './auth'; // 导入auth.js中已配置的api实例

// 创建axios实例
const apiClient = axios.create(axiosConfig);

/**
 * 获取用户列表
 * @returns {Promise<Array>} 用户列表
 */
export const getUsers = async () => {
  try {
    // 添加/api前缀
    const response = await axios.get('http://localhost:8080/api/users');
    console.log('获取用户列表响应:', response);
    return response.data;
  } catch (error) {
    console.error('获取用户列表失败:', error);
    throw error;
  }
};

/**
 * 获取用户详情
 * @param {string} userId - 用户ID
 * @returns {Promise<Object>} 用户详情
 */
export const getUserById = async (userId) => {
  try {
    const response = await apiClient.get(`/users/${userId}`);
    return response.data;
  } catch (error) {
    console.error(`获取用户 ${userId} 详情失败:`, error);
    throw error;
  }
};

/**
 * 创建新用户
 * @param {Object} userData - 用户数据
 * @returns {Promise<Object>} 创建结果
 */
export const createUser = async (userData) => {
  try {
    const response = await apiClient.post('/users', userData);
    return response.data;
  } catch (error) {
    console.error('创建用户失败:', error);
    throw error;
  }
};

// 获取用户统计数据
export const getUserStats = (userId, startDate, endDate) => {
  return apiClient.get(`/users/${userId}/stats`, {
    params: { startDate, endDate }
  });
};

// 获取活动类型分布
export const getActivityTypeDistribution = (userId, startDate, endDate) => {
  return apiClient.get(`/users/${userId}/activity-types`, {
    params: { startDate, endDate }
  });
};

// 获取运动频率统计
export const getWorkoutFrequency = (userId, startDate, endDate) => {
  return apiClient.get(`/users/${userId}/workout-frequency`, {
    params: { startDate, endDate }
  });
};

// 获取运动总结数据
export const getWorkoutSummary = (userId, startDate, endDate) => {
  return apiClient.get(`/users/${userId}/summary`, {
    params: { startDate, endDate }
  });
};

// 获取用户社交互动数据
export const getSocialInteractions = (userId, startDate, endDate) => {
  return apiClient.get(`/users/${userId}/social`, {
    params: { startDate, endDate }
  });
};

// 拦截器处理响应
apiClient.interceptors.response.use(
  (response) => {
    return response;
  },
  (error) => {
    console.error('API请求失败:', error);
    return Promise.reject(error);
  }
);

export default apiClient; 